package cn.swufe.edu.stu.hw_tools.Convert;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import cn.swufe.edu.stu.hw_tools.R;

public class Hui_CV_Activity extends AppCompatActivity implements Runnable{

    Handler handler;
    private ArrayList<HashMap<String, String>> listItems;
    private SimpleAdapter listItemAdapter;
    EditText v;
    double mon;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hui_cv);
        v = findViewById(R.id.CV2editTextText);
        v.setText("0");

        initListView();

        ListView listView = findViewById(R.id.CV2LV);
        listView.setAdapter(listItemAdapter);

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {

                if(msg.what==5){
                    listItems = (ArrayList<HashMap<String,String>>)msg.obj;
                    listItemAdapter = new SimpleAdapter(Hui_CV_Activity.this, listItems, // listItems数据源
                            R.layout.list_item, // ListItem的XML布局实现
                            new String[]{"ItemTitle", "Price"},
                            new int[]{R.id.itemTitle, R.id.price}
                    );
                    listView.setAdapter(listItemAdapter);
                }
                super.handleMessage(msg);
            }
        };
    }

    public void back(View btn){
        Intent intent = getIntent();
        setResult(2,intent);
        finish();
    }

    public void onSearch(View btn){
        try {
            mon = Double.parseDouble(v.getText().toString())*0.01;
            Thread t = new Thread(this);
            t.start();
        } catch (Exception e) {
            mon = 0;
            v.setText("0");
            Toast.makeText(this, "٩(๑˃̵ᴗ˂̵๑)۶又不输入数字是吧？", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void run(){
        //发送消息
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        try {
            Document doc = Jsoup.connect("https://wocha.cn/huilv/?jinri").get();
            Element tables = doc.getElementsByTag("table").first();
            Elements rows = tables.getElementsByTag("tr");
            rows.remove(0);
            for(Element row : rows){
                Elements tds = row.getElementsByTag("td");
                Element td1 = tds.get(0);
                Element td2 = tds.get(5);
                String str1 = td1.text().trim();
                String str2 = String.format("%.4f",Double.parseDouble(td2.text().trim())/100);
                String str3 = String.format("%.4f",Double.parseDouble(str2)*mon)+"元  (汇率为："+str2+")";
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("ItemTitle",str1);
                map.put("Price",str3);
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Message msg =handler.obtainMessage(5,list);
        handler.sendMessage(msg);
    }

    private void initListView() {
        listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 1; i < 10; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "币种" + i); // 标题文字
            map.put("Price", "0元 (汇率为：0.0000)" ); // 详情描述
            listItems.add(map);
        }
        // 生成适配器的Item和动态数组对应的元素
        listItemAdapter = new SimpleAdapter(this, listItems, // listItems数据源
                R.layout.list_item, // ListItem的XML布局实现
                new String[]{"ItemTitle", "Price"},
                new int[]{R.id.itemTitle, R.id.price}
        );
    }
}